<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
    插入系统评价数据
	
  * @author clooney
 *  2011/6/27
 */
class Cli_Valuation extends CLI
{
	public function main()
	{
		/*订单商品数据 */
		$select = DB::select()
			->from('order_trades')
			->where('sys', '=', 1); 
		$iterator = new Db_Iterator($select, 'default', 10);
		$new_array = array();
		CLI::output("开始更新...\n");
		
		$comment_data = array(
			'性价比高不错的' ,'价格非常给力，功能够我使用，很喜欢。。。。' ,'到货后一个星期才写的评价，就是为了写点感受，然后来人有些借鉴，总的来说基本满意', '四个字：物超所值','得到媳妇认可', 
			'不错不错，服务周到，值得购买！！！！！！！！', 
			'非常好的东东，我喜欢。', 
			'第一次华强北在线网购,东西,服务都不错',
			'物美价廉，比本地便宜，用着很好', 
			'正品，好东西，赞一个~' ,
			'买第二个了，都是送人的，适合做礼物', 
			'绝对超值，货真价实，且比卖场便宜 不错 帮同学买的', 
			'华强北在线 很划算的 ',
			'总体来说还是非常不错，价格也可以，推荐！',
			'方便?实惠?太满意了太满意了太满意了',
			'我是帮朋友买的,自己没用,朋友说很好很喜欢,所以给五星', 
			'不错，感觉还行', 
			'东西没什么说的，足够精致，很好O(∩_∩)O~', 
			'用了有好几个月了，挺喜欢的', 
			'商家服务很好，商城很人性化，很给力，继续支持！',
		);
		$i = 0;
		while ($iterator->valid())
		{
			foreach ($iterator->current() as $g)
			{   
				if ( ! empty($g))
				{
				    $i++;
					$order_trades_data = array();
				    //根据商品id取店铺id
					$select_shop = DB::select('shop_id')
			        	->from('goods')
						->where('goods_id', '=', $g['goods_id'])
						->fetch_row();
						
					//根据店铺id取店铺信息
					$shop_datas = DB::select('shop_name', 'member_id')
					    ->from('shops')
						->where('shop_id', '=', $select_shop['shop_id'])
						->fetch_row();
				 
				    $create_time = $g['created'] + 1*24*3600+rand(3000,3600);
					$order_trades_data['buyer_id'] = $g['member_id'];
					$order_trades_data['nickname'] = $g['nickname'];
					$order_trades_data['shop_id']  = $select_shop['shop_id'];
					$order_trades_data['shop_name'] = $shop_datas['shop_name'];
					$order_trades_data['goods_id'] = $g['goods_id'];
					$order_trades_data['goods_title'] = $g['goods_title'];
					$order_trades_data['prop_str'] = $g['prop_str'];
					$order_trades_data['price']    = $g['price'];
					$order_trades_data['buyer_comment_score'] = 1;
					
					$order_trades_data['buyer_comment_time'] =  $create_time;
					$order_trades_data['buyer_flag'] = 1;

					$order_trades_data['is_anonymity'] = 0;
					
					if ($i <= 900)
					{
					    $order_trades_data['buyer_comment_empty'] = 1;
                        $order_trades_data['buyer_comment'] = '商品质量不错，商家态度也非常好，发货迅速。';
					}else{
					    $order_trades_data['buyer_comment_empty'] = 0;
					    $order_trades_data['buyer_comment'] = $comment_data[mt_rand(0,count($comment_data)-1)];
					}
				   
				    $shop_rates = array();
					$shop_rates['shop_id'] = $select_shop['shop_id'];
					$shop_rates['member_id'] = $g['member_id'];
					$shop_rates['des_score'] = 5;
					$shop_rates['service_score'] = 5;
					$shop_rates['express_score'] = 5;
					$shop_rates['created_time'] = $create_time;
					DB::insert('order_rates', array_keys($order_trades_data))->values(array_values($order_trades_data))->execute();
					DB::insert('shop_rates', array_keys($shop_rates))->values(array_values($shop_rates))->execute();
					
					$shop_rate_data = DB::select(DB::expr("avg(des_score) as avgsc"),DB::expr("avg(service_score) as avgse"),
						    DB::expr("avg(express_score) as avgex"))
				            ->from('shop_rates')
				            ->where('shop_id', '=', $select_shop['shop_id'])
				            ->fetch_row();
				    $update_shop_data = array('des_score'=>round($shop_rate_data['avgsc'],1),'service_score'=>round($shop_rate_data['avgse'],1),
				        'express_score'=>round($shop_rate_data['avgex'],1));
				    DB::update('shops')->set($update_shop_data)->where('shop_id', '=', $select_shop['shop_id'])->execute();
						
				    $score_data = array('good_num'=>DB::expr('good_num+1'),'credit_score'=>DB::expr('credit_score+1'));
					DB::update('members')->set($score_data)->where('member_id', '=', $shop_datas['member_id'])->execute();
				   
					CLI::output("第".$i."条更新成功...\n");
				}
			}
			$iterator->next();
		}
		CLI::output("更新完毕...");
	}
}